 
	********************************************************************************
	****
	**** Date: 04.25.2022							 
	**** Update: 07.11.2023					 
	**** Author: JW josephgwright@gmail.com 				 	
	**** NOTE: This program has been executed in Stata 18.0	 
	**** 
	**** Using data files in $dir: 
	****
	****  	pers-use.dta
	****  	cses-temp.dta
	****
	*********************************************************************************
 
 		capture log close
		log using Ch6-Polarization.log, replace
 
 	******************************
	**** Set directory, seed *****
	******************************
		set more off 
		set matsize 1000
		global seed ="984353"
		set scheme plotplain
		cd "$dir"
		
	*******************
	**** Load data ****
	*******************
		use pers-use,clear
		egen c=count(year) if e(sample)==1,by(lid) 		
		gen election = v2xel_elecparl==1 | v2xel_elecpres==1
		gen time = year-1990
		gen lowerprop = v2elparlel==1 if v2elparlel~=.
		gen lowermix = v2elparlel==2 if v2elparlel~=.
		gen lowermajor = v2elparlel==0 if v2elparlel~=.
				
		** Leader time in power **
		gen t = 1 if year==min  
		replace t = year-current_leader_start_year if year==1991 & year~=.
		sort lid year
		bysort lid: replace t=t[_n-1]+1 if lid==lid[_n-1] & t==.
		sum t
		gen lt =ln(t+1)
	
		** Differenced polarization **
		tsset lid year
		gen dpolar = d.polar

		** Initial values **
		local var = "polarization"
		foreach v of local var {
			 qui gen o`v'=(l1`v'+ l2`v')/2 if minyr==year
			 qui replace o`v'=(l1`v')  if minyr==year & o`v'==.
			 qui egen i`v'=max(o`v'),by(lid)
		}		

		** Standardize outcomes **
		local var = "polarization l1polar"
		foreach v of local var {
			 qui sum `v'
			 qui replace `v'=`v' +abs(r(mean))
			 qui sum `v'
			 qui replace `v'=`v'/r(sd)
		}	
		sum polarization l1polar
		swilk polarization l1polar
		egen maxyr  =max(year),by(lid)
		gen devpolar = polar-ipolar if year==maxyr
		
		** Attacks on the State **
		gen ojud = l1v2x_jucon if year==min
		egen ijud = max(ojud),by(lid)
		 alpha v2jupurge v2jupoatck v2jupack,item std gen(attack)
		 replace attack = attack*-1
		 qui sum attack
		 replace attack = (attack +abs(r(min)))/(r(max) + abs(r(min)))
		 sum attack
		 hist attack
		
		** Set globals **
		global d="persparty"
		global c = "ld ivdem2 l1polar election"	
		gen ivdem2=ivdem*10
		
		** Check serial correlation **
		xtset cowcode year
		qui reg polar $d ld ivdem ipolar election
		abar,lags(4)
		qui reg dpolar $d ld ivdem ipolar election
		abar,lags(4)
		qui reg polar $d $c time,cluster(lid)
		abar,lags(4)
		
		** Polarization cases **
		twoway (line polar year if country=="Benin",sort) ///
			(line polar year if country=="United States",sort) ///
			(line polar year if country=="Venezuela",sort legend(lab(1 "Benin") ///
			lab(2 "United States")lab(3 "Venezuela")pos(5)ring(0)col(1)) ///
			xtit(Year)ytit("Polarization (scaled)"))
		gr export "$dir\golden\Ch6-Polarization-Cases.pdf",as(pdf)replace 

		** Polarization tests **
		reg polar $d ld l1polar,cluster(lid)
		est store polar0
		reg polar $d $c time,cluster(lid)
		est store polar1
		reg polar $d $c time pres lowerprop lowermix,cluster(lid)
		est store polar2 
		 * Dynamic panel model *
		reghdfe polarization $d $c,a(cowcode year)cluster(lid)
		est store polar3
		xtreg polarization $d $c i.year,cluster(cowcode)fe
		lincom $d
		predict e_residuals_1, e
		xtistest e_res, lags(1)
		xtistest e_res, lags(2)
		drop e_res
		 * Dynamic panel model post-1st year *
		reghdfe polarization $d $c if year>min,a(cowcode year)cluster(lid)	
		est store polar4
		 * Dynamic panel model post-1st year with 4 lags *
		reghdfe polarization $d $c l2polar l3polar l4polar,a(cowcode year)cluster(lid)	
		est store polar5	
		 * Baseline & dynamic panel + confounders *
		local var = "lnparty i_pop ipi v2pariglef"
		foreach v of local var {
			qui reg polar `v' $d $c time,cluster(lid)
			lincom $d
			qui reghdfe polarization `v' $d $c time,a(cowcode)cluster(lid)
			lincom $d
		}
		 
 		
		* Plot lag DVs *
		label var persparty "Party personalism"
		label var ld "Democracy age"
		label var ivdem2 "Initial democracy"
		label var election "Election year"
		label var time "Time trend"
		label var pres `""Presidential  " "(Parliamentary)""'
		label var lowermix `""Mixed      " "(Majoritarian)""'
		label var lowerprop `""Proportional " "(Majoritarian)""'
		coefplot(polar0, msymbol(P))(polar1, msymbol(S))(polar2, msymbol(T)) (polar3, msymbol(D)), ///
			drop(_cons l1polarization time) grid(glcolor(gs15))xline(0,lpattern(dash)) xlab(-.05(.05).15) ///
			xtitle(Coefficient estimates,size(small)) order($d  )level(95 90) title("Yearly effect", ///
			size(medium)height(6))xsize(2) ysize(3.5)mlabel format(%9.2g) ///
			mlabsize(vsmall)mlabposition(2)mlabgap(*.75) ///
			legend(lab(3 "No covariates")lab(6 "Baseline") lab(9 "Institutions")  lab(12 "Dynamic panel")  ///
			order(3 6  9 12)size(vsmall)pos(6)col(2)ring(1))saving(h1.gph,replace)	
		
		reg devpolar $d ld ipolar,cluster(lid)
		est store tp0
		reg devpolar $d ld ipolar ivdem2 election time ,cluster(lid)
		est store tp1
		reg devpolar $d ld ipolar ivdem2 election  time pres lowerprop lowermix,cluster(lid)
		est store tp2
 		reghdfe devpolar $d ld ivdem2 election ipolar,a(cowcode year)cluster(lid)
		est store tp3
		coefplot(tp0, msymbol(P))(tp1, msymbol(S))(tp2, msymbol(T)) (tp3, msymbol(D)), ///
			drop(_cons ipolarization time) grid(glcolor(gs15))xline(0,lpattern(dash)) xlab(-.15(.15).45) ///
			xtitle(Coefficient estimates,size(small)) order($d  )level(95 90) title("Total effect", ///
			size(medium)height(6))xsize(2) ysize(3.5)mlabel format(%9.2g) ///
			mlabsize(vsmall)mlabposition(2)mlabgap(*.75) ///
			legend(lab(3 "No covariates")lab(6 "Baseline") lab(9 "Institutions")  lab(12 "Dynamic panel")  ///
			order(3 6  9 12)size(vsmall)pos(6)col(2)ring(1))saving(h2.gph,replace)	
		gr combine h1.gph h2.gph,xsize(5)tit("            Party personalism increases polarization")
		gr export "$dir\golden\Ch6-Macro-Polarization-estimates.pdf",as(pdf)replace 
	
		** IFE **
		regife polar persparty election ld ivdem ipolar,ife(cowcode year,1)a(cowcode year)vce(cluster lid)
		regife polar persparty election ld ivdem ipolar,ife(cowcode year,2)a(cowcode year)vce(cluster lid)
 		regife dpolar persparty election ld ivdem ipolar,ife(cowcode year,1)a(cowcode year)vce(cluster lid)
		regife dpolar persparty election ld ivdem ipolar,ife(cowcode year,2)a(cowcode year)vce(cluster lid)		
		
		** Mediate by attacks on the state **
		gen t0=.
		gen tlo=.
		gen thi=.
		gen z0=.
		gen zlo=.
		gen zhi=.
		gen n=_n
				* Yearly change *
		medeff (regress attack $d ld ivdem l1polar election time)  ///
			(regress dpolar $d attack ld ivdem l1polar election time), ///
			mediate(attack)treat($d)sims(1000)vce(cluster lid)
		local b =r(tau)
		replace t0=`b' if n==1
		local b = r(taulo)
		replace tlo=`b'  if n==1
		local b = r(tauhi)
		replace thi=`b'  if n==1
		local b =r(zeta0)
		replace z0=`b'  if n==1
		local b = r(zeta0lo)
		replace zlo=`b'  if n==1
		local b = r(zeta0hi)
		replace zhi=`b'	 if n==1
				* Initial level/total change *
		reg devpolar $d ld ivdem ipolar election time,cluster(lid)
		reg devpolar $d attack ld ivdem ipolar election time,cluster(lid)
		reg attack $d  ld ivdem ipolar election time if devpolar~=.,cluster(lid)
		medeff (regress attack $d ld ivdem ipolar election time)  ///
			(regress devpolar $d attack ld ivdem ipolar election time), ///
			mediate(attack)treat($d)sims(1000)vce(cluster lid)
		local b =r(tau)
		replace t0=`b' if n==2
		local b = r(taulo)
		replace tlo=`b'  if n==2
		local b = r(tauhi)
		replace thi=`b'  if n==2
		local b =r(zeta0)
		replace z0=`b'  if n==2
		local b = r(zeta0lo)
		replace zlo=`b'  if n==2
		local b = r(zeta0hi)
		replace zhi=`b'	 if n==2
	twoway (bar t0 n if n==1,yscale(range(0 .2))barw(.2)ylab(0(.05).2)col(blue*.25) xlab(1 "")) ///
			(bar z0 n if n==1,yscale(range(0 .2))col(blue*.65)barw(.2)legend(off) ///
			xtit(" ")) (bar t0 n if n==2,yscale(range(0 .2))barw(.2)col(blue*.25) ///
			ytit("Marginal effect of" "ruling party personalism")) (bar z0 n if n==2,yscale(range(0 .2)) ///
			col(blue*.65)  barw(.2)xlab(1 " "  2 " ")xscale(range(0.8 2.2)) ///
			text(.1 1.17 "Indirect effect" "via attacks on" "{bf:Judiciary}",size(small)) ///
			text(.175 1.76 "Indirect effect" "via attacks on" "{bf:Judiciary}",size(small)) ///
			tit(Ruling party personalism increases polarization,size(Large)) ///
			subtit(via incumbent attacks on the state,size(small))xlab(1 "Yearly estimate" 2 "Total estimate") ///
			text(.075 1.77 "Direct effect of" "ruling party" "personalism" "on polarization", size(2.2) color(gs4)) ///
			text(.035 1.23 "Direct effect of" "ruling party" "personalism" "on polarization", size(2.2) color(gs4)) ///
			text(.18 2 "31%",size(small))text(.08 2 "69%",size(small))text(.0725 1 "20%",size(small)) text(.035 1 "80%",size(small))) ///
			(pcarrowi .085 1.17 .0725 1.1) (pcarrowi .035 1.16 .035 1.1) 
	gr export "$dir\golden\Ch6-Macro-Polarization-mediation.pdf",as(pdf)replace 
 

*** Merge in cy data ***
	use "$dir\pers-use.dta",clear
	tsset cowcode year
			** Attacks on the State **
			tsset lid year
			alpha v2jupurge v2jupoatck v2jupack,item std gen(cattack)
			replace cattack=cattack*-1
			qui sum cattack
			qui replace cattack = (cattack+abs(r(min)))/(abs(r(min))+r(max))
 			sum cattack
			twoway (line cattack year if country=="Turkey" & year>2000,sort )  ///
				(line cattack year if country=="Poland"& year>2000,sort ylab(0 "Min"  .489328   "Mean"  1 "Max") ///
				legend(lab(1 "Turkey")lab(2 "Poland")pos(5)ring(0)order(1 2))ytit(Attack on the state)xtit(Year) ) 
			gr export "$dir\golden\T-Attack1.pdf",as(pdf)replace 
 			twoway (line cattack year if country=="Turkey" & year>2000,sort )  ///
				(line cattack year if country=="Poland"& year>2000,sort ylab(0 "Min"  .489328   "Mean"  1 "Max") ///
				legend(lab(1 "Turkey")lab(2 "Poland")pos(5)ring(0)order(1 2))ytit(Attack on the state)xtit(Year) ///	
				text(1.035 2016 "2016 failed coup &" "democratic collapse",size(vsmall) )  ///
				text(.5 2013.5 "2013 purge" "judges",size(vsmall) ) ///
				text(.8 2011.5 "2014 justice" "minister power to" "appoint judges",size(vsmall) )) ///
				(pcarrowi .52 2013.5 .7 2013)   (pcarrowi .8 2013.5 .8 2014.7)
			gr export "$dir\golden\T-Attack2.pdf",as(pdf)replace 
			twoway (line cattack year if country=="Turkey" & year>2000,sort )  ///
				(line cattack year if country=="Poland"& year>2000,sort ylab(0 "Min"  .489328   "Mean"  1 "Max") ///
				legend(lab(1 "Turkey")lab(2 "Poland")pos(5)ring(0)order(1 2))ytit(Attack on the state)xtit(Year) ///	
 				text(.3 2011.5 "2015 PiS law" "to purge" "Constitutional" "Court",size(vsmall) ) ///
				text(.62 2014.5 "2018 purge & pack" "National Council of the" "Judiciary (KRS)",size(vsmall) )) ///
				(pcarrowi .31 2012.5 .38 2014.8)   (pcarrowi .64 2016.1 .71 2017.8)		
			gr export "$dir\golden\T-Attack3.pdf",as(pdf)replace 
			drop cattack
		
			local var = "v2jupurge v2jupoatck v2jupack"
			foreach v of local var {
				gen l1`v'=l.`v'
			}
			alpha l1v2jupurge l1v2jupoatck l1v2jupack,item std gen(attack)
			replace attack = attack*-1
			qui sum attack
			replace attack=abs(r(min))+attack
 			hist attack,bin(50)
 			gen ojud = l1v2x_jucon if year==min
			egen ijud = max(ojud),by(lid)
			gen opolar = l1polar if year==min
			egen ipolar = max(opolar),by(lid)
			replace vdem_country = "Republic of Korea" if vdem_country=="South Korea"
			replace vdem_country = "Great Britain" if vdem_country=="United Kingdom"
			replace vdem_country = "Czech Republic" if vdem_country=="Czech Republic"
			replace vdem_country = "Greece" if vdem_country=="Greece"  
	  * Greece 2012; Czech Republic 2010 dropped due to leader being technocratic appointment *
	  * Non-GWF democracy on January 1: Taiwan 1996; Mexico 1997, 2000;  Peru 2000, 2001; Thailand 2007   *
	  * Not coded for leader: Switzerland 1999, 2003, 2007, 2011 *
	  * Not in GWF, too small: Montenegro 2012 *	
	 sort vdem_country year
	 merge vdem_country year using cses-temp
	 tab vdem_country year if _merge==2
	 drop if _merge==2
	 drop _merge
	 keep if polar~=. & attack~=.
	 gen xpolar = (polar+10)/20
	 hist xpolar
	 gen x1polar = xpolar^(1.45)
	 swilk xpolar x1polar if attack~=.
	 hist x1polar
 	  
	 * Descriptive trends *
	 qui sum polarization 
 	 qui egen vpolar = std(polarization)  								/* VDem macro polarization */
	 qui egen cpolar = std(x1polar)										/* CSES micro polarization */
	 sum vpolar cpolar
	 qui egen scpolar = mean(cpolar),by(surveyid)
	 egen stag1 = tag(surveyid) if vpolar~=. & scpolar~=. 
 	 egen std_vpolar=std(vpolar) if stag1==1
	 egen std_scpolar=std(scpolar) if stag1==1
	 sum std_* if stag1==1
	 twoway (lpoly std_vpolar year if stag1==1,bw(4)ylab(-.3(.3).3) ytit(Polarization)tit("Polarization trending upwards"))  ///
		(lpoly std_scpolar year if stag1==1, bw(3.5) xtit(Year,size(small)) ///
		 legend(lab(1 "VDem-polarization")lab(2 "CSES-polarization") ///
		pos(5)ring(0)col(1))note("Standardized variables; N=122.",pos(6)size(vsmall)) )
 	 gr export "$dir\golden\Ch6-Polarization-trends.pdf",as(pdf)replace 
	 
	 * Two measures are within correlated *
	 krls std_scpolar i.cowcode  std_vpolar if stag1==1 
	 reghdfe std_scpolar std_vpolar  if stag1==1,a(cowcode)vce(rob)
	 reghdfe x1polar polarization,a(cowcode)cluster(surveyid)
	 
	 * Kernel regression plot for country averages *
	 krls attack persparty ijud ivdem ld ipolar if stag1==1, 
	 krls scpolar attack ijud ivdem ld ipolar if stag1==1,d(k1)
	 xtreg scpolar attack ijud ivdem ld ipolar if stag1==1, i(cowcode)
	 reghdfe scpolar attack ijud ivdem ld ipolar if stag1==1, a(cowcode)cluster(lid)
	 twoway lpolyci k1_attack persparty,bw(.15)xtit(Ruling party personalism)xlab(0(.2).8)  ///
		ytit(Marginal effect of judicial attacks)legend(off)yline(0,lpat(solid)lcol(gs4)) ///
		yline(.518923,lpat(dash))tit(CSES polarization)
 	 drop k1*
	 
	 egen std_attack =std(attack) if stag==1
	 gen hipers = persparty>.45 if persparty~=.
	 ttest std_attack if stag==1,by(hipers)
 	  
	 * Analysis *
	  gen pXa = persparty*attack
	  global dvar="leftself rightself age female employed union i.income i.urban i.education"
	  global cvar="ld ivdem ijud"
	  global d = "attack"
	  sum x1polar $d persparty $pvar $cvar $dvar

	 * Model selection: scale with country intercepts; cloglog link with log(default) slink *
	 local var  = "logit probit clog loglog logc"
	 local i  =1
	 foreach v of local var {
	 	 qui xi:glm x1polar attack $cvar $dvar,family(binomial)link(`v')vce(cluster surveyid)
		 lincom attack
		 est store p`i'
		 local i = `i'+1
	 }
     estimates table p1 p2 p3 p4 p5,stats(bic)  /* lowest BIC is clog link */
	 gen psample  = e(sample)==1
		 
	 * Build a GMM specification *  No mixed effects
	 qui xi:glm x1polar attack,family(binomial)link(clog)vce(cluster surveyid) 
		 lincom attack
	 qui xi:glm x1polar attack ijud,family(binomial)link(clog)vce(cluster surveyid) 
		 lincom attack
	 qui xi:glm x1polar attack ijud ivdem ld,family(binomial)link(clog)vce(cluster surveyid) 
		 lincom attack
	 qui xi:glm x1polar attack ijud ivdem ld ipolar,family(binomial)link(clog)vce(cluster surveyid) 
		 lincom attack
	 qui xi:glm x1polar attack ijud ivdem ld ipolar $dvar,family(binomial)link(clog)vce(cluster surveyid) 
		 lincom attack
	 qui xi:glm x1polar attack persparty ijud ivdem ld ipolar $dvar,family(binomial)link(clog)vce(cluster surveyid) 
		 lincom attack 
 
	 * Model some heterogeneity *
	 xi:glm x1polar attack persparty pXa $cvar $dvar,family(binomial)link(clog)vce(cluster surveyid) 
			lincom attack + pXa*(.427374 - .188941)
			lincom attack + pXa*(.427374 + .188941)
	  xi:reg x1polar attack persparty pXa $cvar $dvar
			lincom attack + pXa*(.427374 - .188941)
			lincom attack + pXa*(.427374 + .188941)
	  xi:mixed x1polar attack persparty pXa $cvar $dvar || surveyid:
			lincom attack + pXa*(.427374 - .188941)
			lincom attack + pXa*(.427374 + .188941)
 
	 * Baseline no interaction model
	 xi:mixed x1polar attack $cvar $dvar || surveyid:
		 lincom attack
	 est store polar1
	 
	 * Show that (macro) ruling party personalism boosts attacks in this sample *
	 egen stag = tag(surveyid) if e(sample)==1
	 krls attack persparty if stag==1
	 krls attack persparty ld ivdem year ijud if stag==1
	 local var = "i_pop ipolar ipi pres"
	 foreach v of local var {
	 	krls attack persparty ld ivdem year ijud `v' if stag==1
	 }
 
	 * Basline interaction model *
	 xi:mixed x1polar attack persparty pXa $cvar $dvar || surveyid:
		 lincom attack
		 lincom pXa
		 lincom attack + pXa*.18
		 lincom attack + pXa*.70
	 est store polar2
			
					* Robust to specification changes *
				 xi:mixed x1polar attack persparty pXa || surveyid:
				 xi:mixed x1polar attack persparty pXa $cvar || surveyid:
				 xi:mixed x1polar attack persparty pXa $dvar || surveyid:
					 * Robust to changing unit hetero *
				 xi:reg x1polar attack persparty pXa $cvar $dvar
				 xi:reg x1polar i.year attack persparty pXa $cvar $dvar
				 xi:interflex x1polar  attack persparty $cvar $dvar,fe(country)nbin(4)cut(.3 .5 .625)  /* 35%,23%,17%,20% */
				 mat list r(estBin)
				 erase .pdf
	 
	 * Interaction model but exclude respondents who recently selected into parties *
	 xi:mixed x1polar attack persparty pXa $cvar $dvar if newvoter==0 & voteswitch==0 || surveyid:
		 lincom attack
		 lincom pXa
		 lincom attack + pXa*.18
		 lincom attack + pXa*.70	
	 est store polar3
	 
	 label var attack "State attack{sub:c,y}"
	 label var persparty `""Ruling party  " "personalism{sub:c,y}""'
	 label var pXa 	`""Attack x Ruling     " "party personalism{sub:c,y}""'
	 label var pers `""Personalist   " "party voter{sub:i,c,y}""'
	 label var leftself "Left self-id{sub:i,c,y}"
	 label var rightself "Right self-id{sub:i,c,y}"
 
	 coefplot(polar1, msymbol(O))(polar2, msymbol(T))(polar3, msymbol(s)),  ///
		drop(_cons ijud ld ivdem ipolar age female married employed union i.income i.urban i.education _I*)  ///
		order(attack pXa persparty  pers leftself rightself newvoter voteswitcher) ///
		grid(glcolor(gs15))xline(0,lpattern(dash)) xlab(-.2(.1).2)  ///
		xtitle(Coefficient estimates)  level(95 90) ///
		title("Citizen polarization",size(medium)height(6)) ///
		subtit("Comparative Study of Electoral Systems, 1996-2016",pos(6)size(small))xsize(2)ysize(3) mlabel format(%9.2g) ///
		mlabsize(vsmall)mlabposition(2)mlabgap(*.65) ///
		legend(lab(3 "No interaction") lab(6 "Interaction") lab(9 "Exclude new voters" "& party switchers") ///
		pos(6)ring(1)col(3)order(3 6 9)) 
	 gr export "$dir\golden\Ch6-Micro-Polarization-estimates.pdf",as(pdf)replace 

 * Binning estimates *
	global dvar="leftself rightself age female married employed union i.income i.urban i.education"
	global d = "attack"
	gen beta=.
	gen hi=.
	gen lo=.
	gen v=""
	gen n=_n
	capture program drop myplot
	program define myplot
		local var = "attack leftself rightself"
		local i=1
		foreach v of local var {
			margins,dydx(`v')
			local b =r(table)[1,1] 
			local l =r(table)[5,1] 
			local u =r(table)[6,1] 
			replace beta=`b' if n==`i' + $r
			replace lo=`l' if n==`i' + $r
			replace hi=`u' if n==`i' + $r
			replace v="`v'" if n==`i' + $r
			local i=`i'+1
		}

	end
	gen rb=round(beta,.001)
	centile persparty if x1polar~=.,centile(50)
	global cut1 = r(c_1)
 	qui xi:mixed x1polar $d $cvar $dvar if persparty<$cut1 || surveyid:
		global r=0
		myplot
	qui xi:mixed x1polar $d $cvar $dvar if persparty>=$cut1 || surveyid:
		global r=5
		myplot
	centile attack if  x1polar~=.,centile(10 90) /* moving from low (10pctile) to high (90pctile) attack on judiciary is 1.5 units */
	twoway  (scatter beta n if n==1 ,mlab(rb)msym(O)mcol(gs1)legend(lab(1 "Attack on the state")  ///
		lab(3 "Left self-ideology")lab(5 "Right self-ideology")  order(1 3 5)pos(6)ring(0)))  ///
		(rspike hi lo n if n==1,lcol(gs1)lwidth(thin))  (scatter beta n if n==2 ,mlab(rb)msym(S)mcol(gs10) ///
		yscale(range(-.047 .073)) xla(.5 " "  ,nogrid nolabels) xtit("57 surveys in 25 countries",size(small)) ///
		ytit("Marginal effect on polarization",size(small))ylab(-.04(.04).08) xscale(range(0.5 3.5)) ///
		tit("Non-personalist ruling parties") )   (rspike hi lo n if n==3,lcol(gs10)lwidth(thin)yline(0)) ///
		(scatter beta n if n==3,mcol(gs10)msym(T)saving(h1.gph,replace)mlab(rb)) ///
		(rspike hi lo n if n==2,lcol(gs10)lwidth(thin)yline(0))
	twoway  (scatter beta n if n==6 ,mlab(rb)msym(O)mcol(gs1)legend(lab(1 "Attack on the state")  ///
		lab(3 "Left self-ideology")lab(5 "Right self-ideology")  order(1 3 5)pos(6)ring(0)))  ///
		(rspike hi lo n if n==6,lcol(gs1)lwidth(thin))  (scatter beta n if n==7 ,mlab(rb)msym(S)mcol(gs10) ///
		yscale(range(-.047 .073)) xla(5.5 " "  ,nogrid nolabels) xtit("67 surveys in 34 countries",size(small)) ///
		ytit("Marginal effect on polarization",size(small))ylab(-.04(.04).08) xscale(range(5.5 8.5)) ///
		tit("{bf:Personalist} ruling parties") )   (rspike hi lo n if n==7,lcol(gs10)lwidth(thin)yline(0)) ///
		(scatter beta n if n==8,mcol(gs10)msym(T)saving(h2.gph,replace)mlab(rb)) ///
		(rspike hi lo n if n==8,lcol(gs10)lwidth(thin)yline(0))
	gr combine h1.gph h2.gph,col(2) tit(Micro-polarization)
	gr export "$dir\golden\Ch6-Micro-Polarization-bins.pdf",as(pdf)replace 
	drop n beta hi lo v rb
	
	* Check with different two-bin cutpoint *
	 qui xi:mixed x1polar $d $cvar $dvar if persparty<.5 || surveyid:
	 lincom $d
	 qui xi:mixed x1polar $d $cvar $dvar if persparty>=.5 || surveyid:
	 lincom $d
	
	* Check with country fixed effects *
	 xtsum attack persparty if x1polar~=. & pers~=.,i(cowcode)
	 xi:glm x1polar i.country attack persparty pXa $cvar $dvar,family(binomial)link(clog)vce(cluster surveyid)
		 centile persparty if e(sample)==1,centile(10 90)
		 lincom attack
		 lincom pXa
		 lincom attack + pXa*(.16)
		 lincom attack + pXa*(.70)
 
	* Among personalist and nonpersonalist voters *
	xi:mixed x1polar attack persparty pers $cvar $dvar || surveyid:
	qui xi:mixed x1polar attack persparty $cvar $dvar if pers==0 || surveyid:
		lincom attack
	qui xi:mixed x1polar C.attack##C.persparty $cvar $dvar if pers==0|| surveyid:
		lincom c.attack#c.persparty
		lincom attack + c.attack#c.persparty *.18
		lincom attack + c.attack#c.persparty *.70
	qui xi:mixed x1polar attack persparty $cvar $dvar if pers==1 || surveyid:
		lincom attack
	qui xi:mixed x1polar C.attack##C.persparty $cvar $dvar if pers==1 || surveyid:
		lincom c.attack#c.persparty
		lincom attack + c.attack#c.persparty *.18
		lincom attack + c.attack#c.persparty *.70
		
			drop b
			gen b=.
			gen hi=.
			gen lo=.
			gen n=_n
			gen xd = .
		xi:interflex x1polar attack persparty $cvar $dvar if pers==0,cluster(surveyid)type(kernel)bw(.2)
		mat list r(margeff)
		forval i = 1/50 {
			qui replace xd = r(margeff)[`i',1] if n==`i'
			qui replace b = r(margeff)[`i',2] if n==`i'
			qui replace lo = r(margeff)[`i',4] if n==`i'
			qui replace hi = r(margeff)[`i',5] if n==`i'
		}
		twoway (rspike lo hi xd) (scatter b xd,yline(0,lcol(red)lpat(solid))legend(off)saving(h1.gph,replace)ylab(-.1(.05).15) ///
			xtit(Ruling party personalism)ytit(Marginal effect of attack)tit(Established party voters)yscale(range(-.12 .164)))
 		xi:interflex x1polar attack persparty $cvar $dvar if pers==1,cluster(surveyid)type(kernel)bw(.2)
		mat list r(margeff)	
		forval i = 1/50 {
			qui replace xd = r(margeff)[`i',1] if n==`i'
			qui replace b = r(margeff)[`i',2] if n==`i'
			qui replace lo = r(margeff)[`i',4] if n==`i'
			qui replace hi = r(margeff)[`i',5] if n==`i'
		}
		twoway (rspike lo hi xd) (scatter b xd,yline(0,lcol(red)lpat(solid))legend(off)saving(h2.gph,replace)ylab(-.1(.05).15) ///
			xtit(Ruling party personalism)ytit(Marginal effect of attack)tit(Personalist party voters)yscale(range(-.12 .164)))
		erase .pdf
		gr combine h1.gph h2.gph,xsize(8)tit(Attacks on the judiciary increase polarization among all voters)
		erase h1.gph
		erase h2.gph
		drop hi lo n b xd
		gr export "$dir\golden\T-Attack-Micro-Polarization-voter.pdf",as(pdf)replace 
		
		
		
	* Look at Support for Democracy as best form of government *
			* Support for democracy *
			recode B3015 (5 7 8 9=.),gen(demsupport)
			replace demsupport = (demsupport-1)/3
			replace demsupport = demsupport*-1  + 1
			tab B3015 demsupport, m
			table demsupport, stat(n pers) stat(mean pers) stat(n pop)  stat(mean pop)
			
	  xi:mixed demsupport attack $cvar $dvar   || surveyid:
	  centile persparty if e(sample),centile(50)
	  xi:mixed demsupport C.attack##C.pers $cvar $dvar if persparty<.45  || surveyid:
	  xi:mixed demsupport C.attack##C.pers $cvar $dvar if persparty>=.45   || surveyid:
			global dvar="leftself rightself age female married employed union i.income i.urban i.education"
			global d = "attack"
			gen beta=.
			gen hi=.
			gen lo=.
			gen v=""
			gen n=_n
			capture program drop myplot
			program define myplot
				local var = "attack _Iincome_1 _Ieducation_2"
				local i=1
				foreach v of local var {
					margins,dydx(`v')
					local b =r(table)[1,1] 
					local l =r(table)[5,1] 
					local u =r(table)[6,1] 
					replace beta=`b' if n==`i' + $r
					replace lo=`l' if n==`i' + $r
					replace hi=`u' if n==`i' + $r
					replace v="`v'" if n==`i' + $r
					local i=`i'+1
				}

			end
			centile persparty if demsupport~=.,centile(50)
			global cut1 = r(c_1)
			qui xi:mixed demsupport $d $cvar $dvar if persparty<$cut1 || surveyid:
				global r=0
				myplot
			qui xi:mixed demsupport $d $cvar $dvar if persparty>=$cut1 || surveyid:
				global r=5
				myplot
			centile attack if demsupport~=.,centile(10 90) /* moving from low (10pctile) to high (90pctile) attack on judiciary */
			gen rb =round(beta,.01)
			twoway  (scatter beta n if n==1 | n==6 ,mlab(rb) msym(O)mcol(gs1)legend(lab(1 "Attack on the state") ///
				lab(3 "High income (low)")lab(5 "High education (low)") ///
				order(1 3 5)pos(7)ring(0)))  (rspike hi lo n if n==1 | n==6 ,lcol(gs1)lwidth(thin)) ///
				(scatter beta n if n==2 | n==7 ,mlab(rb) msym(S)mcol(gs10)yscale(range(-.12 .10)) xlab(2 "Low" 7  "High") ///
				xtit(Ruling party personalism,size(small)) ytit(Marginal effect,size(small))ylab(-.12(.06).12) ///
				xscale(range(0.5 8.5))tit(Support for Democracy) )  (rspike hi lo n if n==2 | n==7 ,lcol(gs10)lwidth(thin)yline(0)) ///
				(scatter beta n if n==3 | n==8 ,mlab(rb) msym(T)mcol(gs10)) (rspike hi lo n if n==3 | n==8 ,lcol(gs10)lwidth(thin))
			gr export "$dir\golden\Ch6-Support-Democracy-bins.pdf",as(pdf)replace 
			drop n beta hi lo v		
			
	
	* Compare CSES sample to global sample *
	egen cytag=tag(country year)
	keep if cytag==1
	keep cowcode year country ld ivdem ipolar polarization
	sort cowcode year
	merge cowcode year using pers-use
	tab _merge
	gen cses_sample =_merge==3
	ttest gwf_duration,by(cses_sample)
	ttest ivdem,by(cses_sample)
	ttest persparty,by(cses_sample)
					** Attacks on the State **
					gen ojud = l1v2x_jucon if year==min
					egen ijud = max(ojud),by(lid)
					 alpha v2jupurge v2jupoatck v2jupack,item std gen(attack)
					 replace attack = attack*-1
					 qui sum attack
					 replace attack = (attack +abs(r(min)))/(r(max) + abs(r(min)))
					 sum attack
					 hist attack
	ttest ld,by(cses_sample)
	ttest ivdem,by(cses_sample)
	ttest ijud,by(cses_sample)
	ttest attack,by(cses_sample)
	ttest polarization,by(cses_sample)
		
 	 ********* The End **********
	 
	 capture log close
 